﻿@namespace BlazorCharts
@inherits ElementAxes<TData>
@typeparam TData

@if (Chart.IsInit && (AxisGroup?.AxesYLeft == this || AxisGroup?.AxesYRight == this) && Visible == true)
{
    <g class="bc-axes-y" transform="translate(@Rect.LT.ToPoint())">

        @*左右的刻度线文本所在的位置不同*@
        @if (IsSecondaryAxis == false)
        {
            <line x1="@Rect.W" y1="@Rect.H" x2="@Rect.W" y2="0" stroke="rgb(110 110 110)" stroke-width="1" />
        }
        else
        {
            <line x1="0" y1="@Rect.H" x2="0" y2="0" stroke="rgb(110 110 110)" stroke-width="1" />
        }

        @{
            //计算主要刻度步进
            double StepMajor = UnitsMajor ?? 0;
            if (StepMajor == 0)
            {
                StepMajor = Math.Pow(10, (AxesYMax.ToString().Length - 2)) * 5;
                if (AxesYMax / StepMajor > 6)
                    StepMajor = Math.Pow(10, (AxesYMax.ToString().Length - 2)) * 10;
                else if (AxesYMax / StepMajor < 3)
                    StepMajor = Math.Pow(10, (AxesYMax.ToString().Length - 2)) * 2;
            }

            //计算次要刻度步进
            double StepMinor = UnitsMinor ?? (StepMajor / 5);//默认是主要的一半
        }

        @*绘制次要刻度*@
        @if (GridLineMinor == true && IsSecondaryAxis == false)//次要轴不绘制刻度线
        {
            double nowMinorValue = 0;//暂时先不考虑负数问题
            @while (nowMinorValue <= AxesYMax)
            {
                var h = (int)(Rect.H - (double)Rect.H * (nowMinorValue / AxesYMax));
                <line x1="@(Rect.W)" y1="@h" x2="@(Rect.W + AxisGroup.AxesX.Rect.W)" y2="@h" stroke="rgb(242 242 242)" stroke-width="1"></line>
                nowMinorValue += StepMinor;
            }
        }

        @*绘制主要刻度和文字*@
        @{
            double nowMajorValue = 0;//暂时先不考虑负数问题
            while (nowMajorValue < AxesYMax + StepMajor)
            {
                var drawValue = nowMajorValue > AxesYMax ? AxesYMax : nowMajorValue;//如果Stpe不是正好与最大值匹配，那么补上最大值
                var h = (int)(Rect.H - (double)Rect.H * (drawValue / AxesYMax));

                @if (IsSecondaryAxis == false)
                {
                    <line x1="@(Rect.W - 5)" y1="@h" x2="@(Rect.W)" y2="@h" stroke="rgb(110 110 110)" stroke-width="1"></line>
                }
                else
                {
                    <line x1="0" y1="@h" x2="5" y2="@h" stroke="rgb(110 110 110)" stroke-width="1"></line>
                }


                if (LabelPosition == AxesLabelPosition.Axis)
                {//显示文本

                    @*左右的刻度线文本所在的位置不同*@
                    @if (IsSecondaryAxis == false)
                    {
                        <text>
                            <text font-size="@FontSize" fill="rgb(40 40 40)" x="@Rect.W" y="@(h)" transform="translate(@(-DistanceAxis.Value),@(FontSize / 3))" text-anchor="@TextAnchor.End.ToDescription()">@drawValue</text>
                        </text>
                    }
                    else
                    {
                        <text>
                            <text font-size="@FontSize" fill="rgb(40 40 40)" x="0" y="@(h)" transform="translate(@(DistanceAxis.Value),@(FontSize / 3))" text-anchor="@TextAnchor.Start.ToDescription()">@drawValue</text>
                        </text>
                    }
                }

                @if (GridLineMajor == true && IsSecondaryAxis == false)//次要轴不绘制刻度线
                {
                    <line x1="@(Rect.W)" y1="@h" x2="@(Rect.W + AxisGroup.AxesX.Rect.W)" y2="@h" stroke="rgb(217 217 217)" stroke-width="1"></line>
                }
                nowMajorValue += StepMajor;
            }
        }
    </g>


    @*<polyline points="@Rect.ToPoints()" style="fill:rgb(0 255 0 / 0.10);" />*@
}